﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKSparseBreadth.ModelLayer;
using YJKSparseBreadth.Properties;
using YArchitech.LIB;
using YJKGeometryFuncs;
using YJKInspectModelFunc;

namespace YJKSparseBreadth.ViewLayer
{
	public partial class ResultForm : System.Windows.Forms.Form
	{
		public List<AreaCheckResultBean> LstNeedTrust
		{
			get
			{
				return this.m_lstNeedTrust;
			}
			private set
			{
				this.m_lstNeedTrust = value;
			}
		}

		public UIDocument UseUIDocument
		{
			get
			{
				return this.m_useUIDocument;
			}
			private set
			{
				this.m_useUIDocument = value;
			}
		}

		public bool IfIsBackGroundEsc
		{
			get
			{
				bool bIfIsBackGroundEsc = this.m_bIfIsBackGroundEsc;
				this.m_bIfIsBackGroundEsc = false;
				return bIfIsBackGroundEsc;
			}
			private set
			{
				this.m_bIfIsBackGroundEsc = value;
			}
		}

		public bool IfIsClose
		{
			get
			{
				return this.m_bIfIsClose;
			}
			set
			{
				this.m_bIfIsClose = value;
			}
		}

		public bool IfNeedContinue
		{
			get
			{
				bool bIfNeedContinue = this.m_bIfNeedContinue;
				this.m_bIfNeedContinue = false;
				return bIfNeedContinue;
			}
			set
			{
				this.m_bIfNeedContinue = value;
			}
		}

		public ResultForm(List<List<AreaCheckResultBean>> input, UIDocument inputUiDoc, IWin32Window revitHandle)
		{
			this.InitializeComponent();
			this.m_RevitHandle = revitHandle;
			this.m_useUIDocument = inputUiDoc;
			this.dataGridView_Result.AutoGenerateColumns = false;
			this.m_lstlstAllResult = input;
			this.Text = YJKSparseBreadth.Properties.Resources.Str_ResultFormName;
			this.MakeDataTable();
		}

		protected override bool ProcessKeyEventArgs(ref Message m)
		{
			int num = 256;
			int num2 = 27;
			if (m.Msg == num && m.WParam.ToInt32() == num2)
			{
				base.Close();
			}
			return base.ProcessKeyEventArgs(ref m);
		}

		public void AddTrustValue()
		{
			Utility.GetSchema(this.m_useUIDocument.Document, YJKSparseBreadth.Properties.Resources.Str_GUID);
			Transaction transaction = new Transaction(this.m_useUIDocument.Document, "addTrustValue");
			try
			{
				transaction.Start();
				for (int i = 0; i < this.LstNeedTrust.Count; i++)
				{
					this.LstNeedTrust[i].TryAddValue();
				}
				transaction.Commit();
			}
			catch (Exception)
			{
				if (transaction.HasStarted())
				{
					transaction.RollBack();
				}
			}
		}

		private void SendEscapeMessage()
		{
            YJKInspectModelFunc.Utility.SendEscToHandle(this.m_RevitHandle);
			this.IfIsBackGroundEsc = true;
		}

		private void ResultForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
		}

		private void MakeDataTable()
		{
			this.dataGridView_Result.DataSource = null;
			this.m_useDataTable = new DataTable();
			this.m_useDataTable.Columns.Add("LevelName", typeof(string));
			this.m_useDataTable.Columns.Add("FireaAreaName", typeof(string));
			this.m_useDataTable.Columns.Add("RuleValue", typeof(string));
			this.m_useDataTable.Columns.Add("DesignValue", typeof(string));
			this.m_useDataTable.Columns.Add("HashValue", typeof(int));
			foreach (List<AreaCheckResultBean> list in this.m_lstlstAllResult)
			{
				int num = 0;
				foreach (AreaCheckResultBean areaCheckResultBean in list)
				{
					if (areaCheckResultBean.IfDesignWrong)
					{
						if (num == 0)
						{
							this.m_useDataTable.Rows.Add(new object[]
							{
								areaCheckResultBean.UseLevel.Name,
								YJKInspectModelFunc.Utility.GetFireAreaString(areaCheckResultBean.UseArea),
								this.GetSuitStringForResultShow(areaCheckResultBean.RuleValue),
								this.GetSuitStringForResultShow(areaCheckResultBean.DesignValue),
								areaCheckResultBean.GetHashCode()
							});
						}
						else
						{
							this.m_useDataTable.Rows.Add(new object[]
							{
								string.Empty,
								YJKInspectModelFunc.Utility.GetFireAreaString(areaCheckResultBean.UseArea),
								this.GetSuitStringForResultShow(areaCheckResultBean.RuleValue),
								this.GetSuitStringForResultShow(areaCheckResultBean.DesignValue),
								areaCheckResultBean.GetHashCode()
							});
						}
						num++;
					}
				}
			}
			this.dataGridView_Result.DataSource = this.m_useDataTable;
		}

		private AreaCheckResultBean GetResultBeanInDataTable(int rowIndex, bool ifRemove = false)
		{
			AreaCheckResultBean areaCheckResultBean = null;
			int num = (int)this.m_useDataTable.Rows[rowIndex].ItemArray[4];
			for (int i = 0; i < this.m_lstlstAllResult.Count; i++)
			{
				List<AreaCheckResultBean> list = this.m_lstlstAllResult[i];
				for (int j = 0; j < list.Count; j++)
				{
					AreaCheckResultBean areaCheckResultBean2 = list[j];
					if (num == areaCheckResultBean2.GetHashCode())
					{
						areaCheckResultBean = areaCheckResultBean2;
						if (ifRemove)
						{
							list.RemoveAt(j);
						}
					}
					if (areaCheckResultBean != null)
					{
						break;
					}
				}
				if (areaCheckResultBean != null)
				{
					break;
				}
			}
			return areaCheckResultBean;
		}

		private string GetSuitStringForResultShow(double input)
		{
			if (Geometry.LessThan(input, Utility.ValueZero))
			{
				return YJKSparseBreadth.Properties.Resources.Str_Des_NoneRuleValue;
			}
			return string.Format("{0:F}", input) + "m";
		}

		private void button_Cancle_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void btn_Trust_Click(object sender, EventArgs e)
		{
			List<int> list = new List<int>();
			int num = 0;
			IEnumerator enumerator = ((IEnumerable)this.dataGridView_Result.Rows).GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					if ((bool)((DataGridViewRow)enumerator.Current).Cells[4].EditedFormattedValue)
					{
						list.Add(num);
					}
					num++;
				}
			}
			foreach (int rowIndex in list)
			{
				AreaCheckResultBean resultBeanInDataTable = this.GetResultBeanInDataTable(rowIndex, true);
				if (resultBeanInDataTable != null)
				{
					this.LstNeedTrust.Add(resultBeanInDataTable);
				}
			}
			this.MakeDataTable();
		}

		private void Event_GridViewDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			int rowIndex = e.RowIndex;
			this.IfNeedContinue = true;
			if (rowIndex != -1)
			{
				AreaCheckResultBean resultBeanInDataTable = this.GetResultBeanInDataTable(rowIndex, false);
				ElementSet elementSet = new ElementSet();
				List<Element> list = new List<Element>();
				elementSet.Insert(resultBeanInDataTable.UseArea);
				list.Add(resultBeanInDataTable.UseArea);
				this.UseUIDocument.ShowElements(elementSet);
				RevitVersionFuncs.SetSelection(this.UseUIDocument.Selection, list);
				this.UseUIDocument.RefreshActiveView();
			}
		}

		private void dataGridView_Result_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			this.Event_GridViewDoubleClick(sender, e);
		}

		private void ResultForm_FormClosed(object sender, FormClosedEventArgs e)
		{
		}

		private IWin32Window m_RevitHandle;

		private UIDocument m_useUIDocument;

		private List<AreaCheckResultBean> m_lstNeedTrust = new List<AreaCheckResultBean>();

		private List<List<AreaCheckResultBean>> m_lstlstAllResult = new List<List<AreaCheckResultBean>>();

		private DataTable m_useDataTable;

		private bool m_bIfIsBackGroundEsc;

		private bool m_bIfIsClose;

		private bool m_bIfNeedContinue;
	}
}
